<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            
            <p>当前操作
                <span style="font-size: 20px;color:#fd2f2f;">{{start_time}}</span>  
                的采购成本。

                <el-date-picker
                    v-model="start_time2"
                    type="month"
                    placeholder="选择月份">
                </el-date-picker>
                <el-button type="primary" plain size="small" @click="change_month" >切换月份</el-button>
            </p>
            
            
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-upload
                        class="upload-demo"
                        ref="upload"
                        :action="api.upload"
                        :on-preview="handlePreview"
                        :on-success="handleUploadSuccess"
                        :on-error="handleUploadError"
                        :auto-upload="false">
                        <el-button slot="trigger" size="small" type="warning">选择产品成本文件</el-button>
                        <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传</el-button>
                        <el-button class="btn-mar" size="small" type="primary" @click="download_purchase()">{{ start_time }}成本模板下载</el-button>
                    </el-upload>
                </el-col>
                
            </el-row>
            <el-steps :active="1" align-center>
                <el-step title="步骤1" description="下载产品成本"></el-step>
                <el-step title="步骤2" description="填写成本后上传"></el-step>
                <el-step title="步骤3" description="下载产品成本明细并检查成本"></el-step>
                <el-step title="步骤4" description="确认无误后上传"></el-step>
            </el-steps>
            <el-divider></el-divider>
            <el-divider></el-divider>
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="12" :sm="12">
                        <el-form-item label="月份:" prop="month.value">
                            <el-date-picker
                                v-model="queryParams.month.value"
                                type="month"
                                placeholder="选择月份">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item label="编号:" prop="number.value">
                            <el-input v-model="queryParams.number.value" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :md="12" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="resetQuery" size="mini" icon="el-icon-refresh">重置</el-button>

                        </el-form-item>
                    </el-col>
                </el-row>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-form>
            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="primary" @click="handleEdit(scope.row)" v-hasPermi="api.edit">修改采购成本</el-button>
                            <!-- <el-button class="btn-mar" size="mini" type="warning" icon="el-icon-tickets" @click="manual_update(scope.row)">手动更新</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="info" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button> -->
                            <!-- <el-button class="btn-mar" size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button> -->

                        </template>
                    </el-table-column>
                </template>
            </qa-table>

            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData"  :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>

    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm },
    
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '月份',
                    prop: 'month',
                },
                {
                    visible: true,
                    label: '编号',
                    prop: 'number',
                },
                {
                    visible: true,
                    label: '产品成本￥(人名币)',
                    prop: 'material',
                },
                {
                    visible: true,
                    label: '上次修改时间',
                    prop: 'add_time',
                },
            ],
            // 接口地址
            api: {
                index: `/admin/querypurchase/index`,
                find: `/admin/querypurchase/find`,
                add: `/admin/querypurchase/add`,
                edit: `/admin/querypurchase/edit`,
                delete: `/admin/querypurchase/delete`,
                export: `/admin/querypurchase/export`,
                change_month: `/admin/querypurchase/change_month`,
                manual_update: `/admin/Taskorder/update_order_ismaterial_3day`,
                upload: `https://api.fan-zhou.com/admin/querypurchase/uploads`,
                status: `/admin/querypurchase/status`
            },
            editFormParams: {
            },
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
              //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                month: { value:undefined, op: '='},
                number: { value:undefined, op: 'like'},
            },
            start_time2:'',
        }
    },
    created () {
        this.initIndex();
    },
    watch: {
        
    },
    methods: {
        // 案列：当前组件格式化方法
        testFormatter(prop) {
            return prop
        },
        
        // 编辑
        handleEdit(row) {
            this.handleFind(row['id'])
        },

        // 编辑回显
        async handleFind(id) {
            let response = await this.request.get(this.api.edit, { params: { id } })
            this.findFormData = this.formatFindData(response.data)
            this.editOpen = true
        },
        
        manual_update(row) {
            this.$confirm(`确定要更新吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest({ id: row.order_id})
                })
                .catch(err => {})
        },
        baseRequest({ id }) {
            this.loading = true
            this.request
                .post(this.api.manual_update, { id })
                .then(response => {
                    if(response.code == 1){
                        this.$message.success('更新成功');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
        download_purchase() {
            this.loading = true
            const qyparams = {
                start_time: this.start_time,
            }
            this.$confirm(`确定要下载本月产品成本模板吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    return this.request.get('/admin/querypurchase/download_purchase', { params: qyparams, responseType: 'blob' })
                })
                .then(response => {
                    this.download(response, this.start_time+'产品成本模板.xlsx')
                    this.$message.success('下载成功');
                    this.initIndex();
                })
                .catch(err => {})
                .finally(() => {
                    this.loading = false
                })
        },
        //上传
        submitUpload() {
            this.loading = true
            this.$refs.upload.submit();
        },
        handlePreview(file) {
            console.log(file);
        },
        
        // 上传成功回调
        handleUploadSuccess(res) {
            this.loading = false
            if (res.code == 1) {
                this.$message.success('上传成功');
                this.initIndex();
            } else {
                this.$message({
                    type: 'error',
                    message: '上传失败'+res.msg,
                })
            }
        },
        // 上传失败
        handleUploadError() {
            this.$message({
                type: 'error',
                message: '上传失败',
            })
        },

        change_month() {
            this.$confirm(`确定要切换操作月份吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    this.baseRequest_change_month({ start_time: this.start_time2})
                })
                .catch(err => {})
        },
        baseRequest_change_month({ start_time }) {
            this.loading = true
            this.request
                .post(this.api.change_month, { start_time })
                .then(response => {
                    if(response.code == 1){
                        this.$message.success('切换成功');
                    }
                    this.initIndex();
                })
                .finally(() => {
                    this.loading = false
                })
        },
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
